define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            const searchParams = Controller.api.splitUrlSearch(location.search);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mbti/result/index' + location.search,
                    add_url: 'mbti/result/add?scene_id='+searchParams?.scene_id || 0,
                    edit_url: 'mbti/result/edit',
                    del_url: 'mbti/result/del',
                    multi_url: 'mbti/result/multi',
                    import_url: 'mbti/result/import',
                    table: 'mbti_result',
                }
            });

            Table.button = {
                edit: {
                    name: 'edit',
                    icon: 'fa fa-pencil',
                    title: __('Edit'),
                    extend: 'data-toggle="tooltip" data-container="body" data-area=["80%","80%"]',//调整弹窗尺寸
                    classname: 'btn btn-xs btn-success btn-editone'
                },
                del: {
                    name: 'del',
                    icon: 'fa fa-trash',
                    title: __('Del'),
                    extend: 'data-toggle="tooltip" data-container="body"',
                    classname: 'btn btn-xs btn-danger btn-delone'
                },
                dragsort: {
                    name: 'dragsort',
                    icon: 'fa fa-arrows',
                    title: __('Drag to sort'),
                    extend: 'data-toggle="tooltip"',
                    classname: 'btn btn-xs btn-primary btn-dragsort'
                }
            };

            var table = $("#table");
            $(document).on("click", "#resetFromSearch", function () {
                $(".form-commonsearch [type=reset]").trigger("click");
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'group_name', title: __('Group Name'), operate: 'LIKE'},
                        {field: 'mbti_ie', title: __('Mbti_value'), operate: false, formatter: Controller.api.formatter.mbti},
                        {field: 'sex', title: __('Sex'), searchList: {"male":__('Male'), "female":__('Female')}, formatter: Table.api.formatter.normal},
                        {field: 'relation_type', title: __('Relation_type'), searchList: {"one":__('One'),"group":__('Group')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter:{
                mbti: function (value, row, index) {
                    return `${value}${row.mbti_sn}${row.mbti_tf}${row.mbti_pj}`
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            splitUrlSearch: function(search=""){
                search = search.indexOf("?") > -1 ? search.replace("?", "") : search;
                const searchArr = search.split("&");
                const result = {};
                searchArr.forEach((item)=>{
                    const itemArr = item.split("=");
                    result[itemArr[0]] = itemArr[1];
                });
                return result;
            }
        }
    };
    return Controller;
});
